$(function ()
{
    var vue=new Vue({
        el:"#app",
        data:{
            //loginSeller:"",
            goods:{
                goodsDesc: {itemImages:[],customAttributeItems:[],specificationItems:[]},
                category1Id:"",
                category2Id:"",
                category3Id:"",
                typeTemplateId:"",
                brandId:"",
                itemList:[],
                isEnableSpec:"0"
            },
            picture:{},
            itemCatList1:[],
            itemCatList2:[],
            itemCatList3:[],
            brandList:[],
            specification:[]
        },
        methods:{
            saveOrUpdate:function ()
            {
                var url="save";
                if(this.goods.sellerId)
                {
                    url="update";
                }

                this.goods.goodsDesc.introduction=editor.html();

                axios.post("/goods/"+url,this.goods).then(function (response)
                {
                    if(response.data)
                    {
                        vue.goods={goodsDesc:{itemImages:[],customAttributeItems:[]},category1Id:"",category2Id:"",category3Id:"",typeTemplateId:"",brandId:""};
                        //清除富文本中的内容
                        editor.html('');
                    }
                    else
                    {
                        alert("保存失败！！！");
                    }
                })
            },
            //文件异步上传
            uploadImg:function ()
            {
                //创建表单对象
                var formData=new FormData();
                //追加需要上传的文件
                formData.append("file",file.files[0]);
                //发送异步请求上传文件
                axios({
                    method:"post",//请求方式
                    url:"/upload",//请求URL
                    data:formData,//表单数据
                    headers:{"Content-Type":"multipart/form-data"}//请求头
                }).then(function (response)
                {
                    if(response.data.status==200)
                    {
                        vue.picture.url=response.data.url;
                        console.log(vue.picture);
                    }
                    else
                    {
                        alert("上传图片失败！");
                    }
                })
            },
            addItemImages:function ()
            {
                this.goods.goodsDesc.itemImages.push(this.picture);
            },
            delItemImage:function (index)
            {
                this.goods.goodsDesc.itemImages.splice(index,1);
            },
            findByParentId:function (parentId,itemCatList)
            {
                axios.get("/itemCat/findByParentId?parentId="+parentId).then(function (response)
                {
                    vue[itemCatList]=response.data;
                })
            },
            selectOption:function (event, specName, optionName)
            {
                /*[{"attributeValue":["64G","128G"],"attributeName":"机身内存"}]*/
                
                //检查goods.goodsDesc.specificationItems中attributeName是否存在，存在并返回
                var attArr=this.checkAttribute(this.goods.goodsDesc.specificationItems, "attributeName", specName);
                //attributeName存在的话，
                if(attArr)
                {
                    if(event.target.checked)
                    {
                        //选中就添加
                        attArr.attributeValue.push(optionName);
                    }
                    else
                    {
                        //未选中就删除
                        var index2 = attArr.attributeValue.indexOf(optionName);
                        attArr.attributeValue.splice(index2,1);
                        //若是全部删除，把上一级也要删除
                        if(attArr.attributeValue.length == 0)
                        {
                            var  ItemsArr=this.goods.goodsDesc.specificationItems;
                            var index1=ItemsArr.indexOf(attArr);
                            ItemsArr.splice(index1,1);
                        }
                    }
                }
                //attributeName不存在的话，
                else
                {
                    this.goods.goodsDesc.specificationItems.push({attributeValue:[optionName],attributeName:specName});
                }
            },
            checkAttribute:function (scope, key, value)
            {
                for(var i=0;i<scope.length; i++)
                {
                    if(scope[i][key]==value)
                    {
                        //console.log("已经有了");
                        return scope[i];
                    }
                }
            },
            showSpecifications:function ()
            {
                //初始化 goods.itemList
                this.goods.itemList=[{spec:{}, price: 0, num:999, status:'0', isDefault:'0'}];
                //引入goods.goodDesc.specificationItems
                var specifications=this.goods.goodsDesc.specificationItems;
                //循环遍历goods.goodDesc.specificationItems，添加到goods.itemList
                for(var i=0; i<specifications.length;i++)
                {
                    //添加新的specification
                    this.goods.itemList=this.addSpecifications(this.goods.itemList,specifications[i].attributeValue,specifications[i].attributeName);
                }
                //若是一个都没选，清空goods.itemList
                if(specifications.length==0)
                {
                    this.goods.itemList=[];
                }
            },
            addSpecifications:function (itemList, attributeValue, attributeName)
            {
                //定义一个临时tempTerms,存放中间的每个元素
                var tempTerms=[];

                //遍历原来的 itemList
                for(var i=0; i<itemList.length;i++)
                {
                    //获取原来itemList元素
                    var item=itemList[i];
                    //遍历attributeValue的元素，依次添加
                    for(var j=0;j<attributeValue.length;j++)
                    {
                        //克隆原来的元素，不克隆的话，只能保留最后一次的数据，会覆盖原来的数据
                        var item=JSON.parse(JSON.stringify(item));
                        //添加新的attributeValue, attributeName
                        item.spec[attributeName]=attributeValue[j];
                        //console.log(itemList[i].spec[attributeName]);
                        //添加新的specification
                        tempTerms.push(item);
                    }
                }
                //返回新的specification
                return tempTerms;
            }
        },
        //监视值改变后，马上更新下拉列表
        watch:{
            "goods.category1Id":function (instant)
            {
                this.goods.category2Id="";
                this.itemCatList2=[];
                if(instant)
                {
                    this.findByParentId(instant,"itemCatList2");
                }
            },
            "goods.category2Id":function (instant)
            {
                this.goods.category3Id="";
                this.itemCatList3=[];
                if(instant)
                {
                    this.findByParentId(instant,"itemCatList3");
                }
            },
            //选择到第三个目录时，才现实模板id
            "goods.category3Id":function (instant)
            {
                this.goods.typeTemplateId="";

                if(instant)
                {
                    for(var i=0;i<this.itemCatList3.length;i++)
                    {
                        var itemCat=this.itemCatList3[i];
                        if(instant == itemCat.id)
                        {
                            this.goods.typeTemplateId = itemCat.typeId;
                            break;
                        }
                    }
                }
            },
            "goods.typeTemplateId":function (instant)
            {
                this.goods.brandId="";
                this.brandList=[];
                this.goods.goodsDesc.customAttributeItems=[];
                if(instant)
                {
                    axios.get("/typeTemplate/findOne?typeTemplateId="+instant).then(function (response)
                    {
                        vue.brandList=JSON.parse(response.data.brandIds);
                        vue.goods.goodsDesc.customAttributeItems=JSON.parse(response.data.customAttributeItems);
                    });

                    /*[{"id":27,"text":"网络",options:[]}]*/
                    axios.get("/typeTemplate/findSpecification?typeTemplateId="+instant).then(function (response)
                    {
                        vue.specification=response.data;
                    })
                }
            }
        },
        created:function ()
        {
            this.loginSeller=sessionStorage.getItem("loginSeller");
            this.findByParentId(0,"itemCatList1");
        }
    });
});